package demo1.Factory;
import demo1.Factory.product.Product1;
import demo1.Factory.product.impl.ConcreteProductA;
import demo1.Factory.product.impl.ConcreteProductB;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @ClassName FactoryMethods 工厂方法模式： 一个工厂生产一个具体产品
 * @Description TODO
 * @Author 耀酱
 * @Date 2023/6/2 10:52
 * @Version 1.0
 */
// 工厂方法模式： 一个工厂生产一个具体产品
public class FactoryMethods {
    // 抽象工厂  具体工厂  抽象产品   具体产品
    public static AbstractFactory1 createProduct(Class tClass) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Constructor constructor = tClass.getConstructor();
        return (AbstractFactory1)constructor.newInstance();
    }

    public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        AbstractFactory1 product = FactoryMethods.createProduct(Factory3.class);
        Product1 product1 = product.createProduct();
        product1.append1();

        AbstractFactory1 product2 = FactoryMethods.createProduct(Factory4.class);
        Product1 product22 = product2.createProduct();
        product22.append1();
    }
}
// 抽象工厂
interface AbstractFactory1{
    Product1 createProduct();
}
// 具体工厂1  一个工厂生产一个具体产品
class Factory3 implements AbstractFactory1{

    public Factory3(){

    }
    @Override
    public Product1 createProduct() {
        System.out.println("生产product1产品");
        return new ConcreteProductA();
    }
}
// 工厂2
class Factory4 implements AbstractFactory1{
    public Factory4(){}
    @Override
    public Product1 createProduct() {
        System.out.println("生产product1产品");
        return new ConcreteProductB();
    }
}
